﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Cashier.pos
{
    public partial class UpdateShipmentForm : Form
    {
        private PosForm posform;
        private String columnname;
        private bool isUpdate = false;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="posform"></param>
        /// <param name="columnname">列名</param>
        /// <param name="goodsname">商品名称</param>
        /// <param name="oldvalue">原值</param>
        public UpdateShipmentForm(PosForm posform, String columnname, String goodsname, Object oldvalue)
        {
            InitializeComponent();
            this.posform = posform;
            this.columnname = columnname;

            label_goodsname.Text = goodsname;
            String labelname = String.Empty;
            if (columnname.Equals("price_sale"))
                labelname = "价格";
            else if (columnname.Equals("numbers"))
                labelname = "数量";
            label_old.Text = "旧出货" + labelname;
            label_new.Text = "新出货" + labelname;
            textBox_old.Text = oldvalue.ToString();
            textBox_new.Text = oldvalue.ToString();
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_ok_Click(object sender, EventArgs e)
        {
            isUpdate = true;
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 输入框按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_new_KeyPress(object sender, KeyPressEventArgs e)
        {
            int keyValue = (int)e.KeyChar;
            if (keyValue == 46)
            {
                if (columnname.Equals("numbers"))
                    e.Handled = true;
                else
                    e.Handled = false;
            }
            else if ((keyValue > 47 && keyValue < 58) || keyValue == 8)
            {
                e.Handled = false;
            }
            else if (keyValue == 13)
                e.Handled = false;
            else
                e.Handled = true;
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateShipmentForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            String newVal = textBox_new.Text.Trim();
            String re = null;
            if (columnname.Equals("price_sale"))
                re = @"^\d+(\.\d+)?$";
            else if (columnname.Equals("numbers"))
                re = @"^\d+$";

            if (isUpdate && !Regex.IsMatch(newVal, re))
            {
                e.Cancel = true;
                isUpdate = false;
            }

            if (isUpdate)
            {
                Object value = null;                
                if (columnname.Equals("price_sale"))
                    value = Decimal.Parse(newVal);
                else if (columnname.Equals("numbers"))
                    value = Int32.Parse(newVal);
                posform.UpdateDataGridViewCell(columnname, value);
            }
        }

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    isUpdate = false;
                    this.Close();
                    break;
                case Keys.Enter:
                    isUpdate = true;
                    if (!button_ok.Focused)
                        this.Close();
                    break;
            }
            return b;
        }
    }
}
